package com.snowfield.lb47.lbconfig.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.snowfield.lb47.lbconfig.domain.BlackwhiteList;
import com.snowfield.lb47.lbconfig.service.IBlackwhiteListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 黑白名单配置Controller
 *
 * @author snowfield
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/lbconfig/blackwhitelist")
public class BlackwhiteListController extends BaseController {
    @Autowired
    private IBlackwhiteListService blackwhiteListService;

    /**
     * 查询黑白名单配置列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelist:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlackwhiteList blackwhiteList) {
        startPage();
        List<BlackwhiteList> list = blackwhiteListService.selectBlackwhiteListList(blackwhiteList);
        return getDataTable(list);
    }

    /**
     * 导出黑白名单配置列表
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelist:export')")
    @Log(title = "黑白名单配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlackwhiteList blackwhiteList) {
        List<BlackwhiteList> list = blackwhiteListService.selectBlackwhiteListList(blackwhiteList);
        ExcelUtil<BlackwhiteList> util = new ExcelUtil<BlackwhiteList>(BlackwhiteList.class);
        util.exportExcel(response, list, "黑白名单配置数据");
    }

    /**
     * 获取黑白名单配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(blackwhiteListService.selectBlackwhiteListById(id));
    }

    /**
     * 新增黑白名单配置
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelist:add')")
    @Log(title = "黑白名单配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlackwhiteList blackwhiteList) {
        if (!blackwhiteListService.checkNameUnique(blackwhiteList)) {
            return AjaxResult.error("新增名单 '" + blackwhiteList.getListName() + "'失败，名称已经存在");
        }
        return toAjax(blackwhiteListService.insertBlackwhiteList(blackwhiteList));
    }

    /**
     * 修改黑白名单配置
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelist:edit')")
    @Log(title = "黑白名单配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlackwhiteList blackwhiteList) {
        if (!blackwhiteListService.checkNameUnique(blackwhiteList)) {
            return AjaxResult.error("修改名单 '" + blackwhiteList.getListName() + "'失败，名称已经存在");
        }
        return toAjax(blackwhiteListService.updateBlackwhiteList(blackwhiteList));
    }

    /**
     * 删除黑白名单配置
     */
    @PreAuthorize("@ss.hasPermi('lbconfig:blackwhitelist:remove')")
    @Log(title = "黑白名单配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blackwhiteListService.deleteBlackwhiteListByIds(ids));
    }
}
